<template>
	<view :class="Visible ? 'usable-coupon open' : 'usable-coupon close'">
		<view class="popup-bg" @click="closePopup"></view>
		<view class="main" v-on:click.stop>
			<view class="pop-title">
				<view>{{$t('product.discount0')}}</view>
				<text class="pop-close icon iconfont icon-guanbi" @click.stop="closePopup"></text>
			</view>
			<view class="p-0-20">
				<view class="" v-if="discount.productReduce.length > 0">
					<view class="f22 gray3 line-h-50 p-20-0">
						<text class="text-box ">{{$t('product.full_reduction')}}</text>
						<text v-for="(item,index) in discount.productReduce" :key="index" :label="item" :value="index">
							<text class="ml20" v-if="item.fullType == 1">{{$t('product.min_amount',{place1:item.fullValue})}}</text>
							<text class="ml20" v-if="item.fullType == 2">{{$t('product.min_quantity',{place1:item.fullValue})}}</text>
							<text v-if="item.reduceType == 1">{{$t('product.discount_amount',{place1:item.reduceValue})}}</text>
							<text v-if="item.reduceType == 2">{{(100 - item.reduceValue)/10}}{{$t('product.discount')}}</text>
						</text>
					</view>
				</view>
				<view class="p-20-0 line-h-50 f22 gray3 " v-if="discount.givePoints>0">
					<text class="text-box">{{$t('product.earn_points')}}</text>{{$t('product.cashback_points')}}{{discount.givePoints}}{{$t('product.points')}}
				</view>
			</view>
			<scroll-view scroll-y="true" class="scroll-Y scroll-coupon" :style="'height:' + scrollviewHigh + 'px;'">
				<view style="background-color: #f2f2f2;">
					<view class=" scroll-coupon-tit">{{$t('product.claim_coupon0')}}</view>
					<view v-for="(item, index) in datalist" :key="index" class="item-wrap">
						<view
							:class="item.isGet?'coupon-item coupon-item-gray':'coupon-item coupon-item-'+item.color.text">
							<view class="operation d-b-c">
								<view class="flex-1 coupon-content">
									<view class="mb20">
										<text class="f40 fb">{{ item.name }}</text>
									</view>

									<view class="f22 gray9 mb20">
										<template v-if="item.expireType == 10">
											{{$t('product.validity_period')}}{{ item.expireDay }}{{$t('product.valid_within_days')}}
										</template>
										<template v-if="item.expireType == 20">
											{{$t('product.valid_until')}}{{ item.startTime.text }}$t('product.valid_until_date',{place1: item.endTime.text })
										</template>
									</view>
									<view v-if="item.couponType == 20" class="f22">{{ item.maxPrice > 0 ? $t('product.max_deduction') + item.maxPrice * 1 + $t('product.currency') : $t('product.no_max_deduction') }}</view>
								</view>
								<view class="right-box d-c-c d-c">
									<view class="theme-price mb10" v-if="item.couponType == 10">
										<text class="f24">HK$</text>
										<text class="f52 fb">{{ item.reducePrice * 1 }}</text>
									</view>
									<view class="mb10 theme-price" v-if="item.couponType == 20">
										<text class="f52 fb">{{ item.discount }}</text>
										<text class="f24">{{$t('product.discount')}}</text>
									</view>
									<view class="f24 mb10">{{ item.minPrice > 0 ? $t('product.min_amount_for_use') + item.minPrice * 1 + $t('product.amount_available') : $t('product.no_threshold') }}</view>
									<view class="f26 coupon-btn theme-btn" v-if="!item.isReceive" @click="selectCoupon(item, index)">{{$t('product.claim_now')}}</view>
									<view v-else class="f26 coupon-btn btn-gray white"><text>{{$t('product.already_claimed')}}</text></view>
								</view>
								
							</view>

						</view>
						<view
							class="range_item d-b-c"
							v-if="item.applyRange == 20"
							@click.stop="gotoPage('/pages/coupon/detail?coupon_id=' + item.couponId + '&apply_range=' + item.applyRange)"
						>
							<view class="gray9 f24">
								{{$t('product.restricted_products')}}
								<text class="icon iconfont icon-you" style="color: #999999; font-size: 22rpx;"></text>
							</view>
							<view class="gray9 f24">{{$t('product.no_transfer')}}</view>
						</view>
						<view
							class="range_item d-b-c"
							v-else-if="item.applyRange == 30"
							@click.stop="gotoPage('/pages/coupon/detail?coupon_id=' + item.couponId + '&apply_range=' + item.applyRange)"
						>
							<view class="gray9 f24">
								{{$t('product.specific_category')}}
								<text class="icon iconfont icon-you" style="color: #999999; font-size: 22rpx;"></text>
							</view>
							<view class="gray9 f24">{{$t('product.no_transfer')}}</view>
						</view>
						<view class="range_item d-b-c" v-else>
							<view class="gray9 f24">
								{{$t('product.universal')}}
								<text class="icon iconfont icon-you" style="color: #999999; font-size: 22rpx;"></text>
							</view>
							<view class="gray9 f24">{{$t('product.no_transfer')}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/*手机高度*/
				phoneHeight: 0,
				/*可滚动视图区域高度*/
				scrollviewHigh: 0,
				/*是否可见*/
				Visible: false,
				/*优惠券列表*/
				datalist: {},
				/*尺寸比例*/
				ratio: 1
			};
		},
		props: ['isCoupon', 'couponList', 'discount'],

		onLoad() {},
		mounted() {
			this.init();
		},
		watch: {
			isCoupon: function(n, o) {
				if (n != o) {
					this.Visible = n;
					this.datalist = this.couponList;
					this.getHeight();
				}
			}
		},
		methods: {
			/*初始化*/
			init() {
				let self = this;
				uni.getSystemInfo({
					success(res) {
						self.phoneHeight = res.windowHeight;
						self.ratio = res.windowWidth / 750;
						self.getHeight();
					}
				});
			},
			/*获取高度*/
			getHeight() {
				let count = Object.keys(this.couponList).length;
				if (count > 2) {
					this.scrollviewHigh = this.phoneHeight * 0.5;
				} else {
					if (count == 1) {
						this.scrollviewHigh = 250 * this.ratio + 60;
					} else if (count == 2) {
						this.scrollviewHigh = 460 * this.ratio + 60;
					}
				}
			},
			/*选择优惠券*/
			selectCoupon(e, i) {
				let self = this;
				uni.showLoading({
					title: this.$t('product.claiming')
				});
				self._post('user/coupon/receive', {
					couponId: e.couponId,
				}, function(res) {
					uni.hideLoading();
					uni.showToast({
						title: self.$t('product.claim_successful'),
						duration: 2000,
						icon: 'success'
					});
					self.datalist[i].isReceive = true;
					console.log('1',self.datalist);
				});
				// self.getData();
			},
			/*关闭弹窗*/
			closePopup() {
				this.$emit('close');
			}
		}
	};
</script>

<style lang="scss" scoped>
.usable-coupon .popup-bg {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background: rgba(0, 0, 0, 0.6);
	z-index: 99;
}

.usable-coupon .main {
	position: fixed;
	width: 100%;
	bottom: 0;
	min-height: 200rpx;
	background-color: #fff;
	transform: translate3d(0, 1980rpx, 0);
	transition: transform 0.2s cubic-bezier(0, 0, 0.25, 1);
	padding-bottom: env(safe-area-inset-bottom);
	border-radius: 15rpx 15rpx 0 0;
	z-index: 99;
}

.usable-coupon .main {
	position: fixed;
	width: 100%;
	bottom: 0;
	min-height: 200rpx;
	background-color: #fff;
	transform: translate3d(0, 1980rpx, 0);
	transition: transform 0.2s cubic-bezier(0, 0, 0.25, 1);
	bottom: env(safe-area-inset-bottom);
	z-index: 99;
}

.usable-coupon.open .main {
	transform: translate3d(0, 0, 0);
}

.usable-coupon.close .popup-bg {
	display: none;
}

.b-radio {
	border: 1rpx solid #ffffff;
	border-radius: 30rpx;
	padding: 10rpx 30rpx;
}
.text-sheng {
	display: inline-block;
	width: 26rpx;
	height: 28rpx;
	background: linear-gradient(180deg, #ff5649 0%, #ff5649 100%);
	border-radius: 3rpx;
	font-size: 18rpx;
	font-family: PingFangSC;
	font-weight: 500;
	color: #ffffff;
	text-align: center;
	line-height: 28rpx;
}

.text-box {
	padding: 2rpx 6rpx;
	background-color: #fbe9e7;
	color: #fd5342;
	margin-right: 10rpx;
	border-radius: 4rpx;
	font-size: 20rpx;
}

.line-h-50 {
	line-height: 50rpx;
}

.pop-title {
	padding-top: 53rpx;
	font-size: 28rpx;
	font-family: PingFangSC;
	font-weight: 500;
	color: #333333;
	position: relative;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-bottom: 50rpx;
}

.pop-close {
	position: absolute;
	right: 20rpx;
	top: 20rpx;
	width: 60rpx;
	height: 60rpx;
	color: #999999;
	display: flex;
	justify-content: center;
	align-items: center;
	.icon-guanbi {
		font-size: 26rpx;
	}
}

.scroll-coupon {
	margin: 0 20rpx;
	border-top: 1rpx solid #eeeeee;
	width: 710rpx;
	box-sizing: border-box;
}

.scroll-coupon-tit {
	height: 74rpx;
	display: flex;
	justify-content: flex-start;
	align-items: center;
	font-size: 24rpx;
	color: #cccccc;
	background-color: #fff;
}
.item-wrap{
	border: 1px solid #eee;
}
</style>
